package com.cn.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cn.exception.BusinessException;
import com.cn.response.ResultCode;
import com.cn.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.mapper.SysUserMapper;
import com.cn.pojo.SysUser;
import com.cn.service.SysUserService;
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService{

    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public SysUser loginUserByCardNo(String username) {
        return this.baseMapper.loginUserByCardNo(username);
    }

    @Override
    public String loginUserByCard(String username, String password) {
        String token=null;
        SysUser sysUser=sysUserMapper.loginUserByCardNo(username);
        if (username!=null){
            String cardPassword=sysUser.getPassword();
            if (password.equals(cardPassword)){
                token= JWTUtils.sign(username,password);
            }else {
                throw new BusinessException(ResultCode.USER_CREDENTIALS_ERROR.getCode(),
                        ResultCode.USER_CREDENTIALS_ERROR.getMessage());
            }
        }else {
            throw new  BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),
                    ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
        }
        return token;
    }
}
